import java.io.File;
import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.Scanner;
import java.util.HashMap;;

public class Lab13 {

	/**
	 * A method that takes in a string "message" and returns a string that has
	 * all of the words of the input string in reverse order. Use a stack to
	 * accomplish this.
	 * 
	 * @param message
	 *            - the input string
	 * @return result
	 */
	public static String printBack(String message) {
		if (message == null) return null;
		
		String words[]  = message.split("\\s+");
		Stack<String> strStack = new Stack<String>();
		for (String word : words) {
			strStack.push(word.replaceAll("[^A-Za-z0-9]", ""));
		}
		
		String ret = "";
		while (!strStack.isEmpty()) {
			String popStr = strStack.pop();
			if (popStr.equals(""))
				continue;
			
			ret = ret + popStr;
			ret = ret + " ";
		}
		ret = ret.substring(0, ret.length() == 0 ? 0 : ret.length() - 1);
		return ret;
	}
	
	/**
	 * A method that takes in a file written in a specific format: ... 
	 * 1 James Grant Temes Warshaw Weber vs. Schnabel Mcnair Cosgrove Braun Crouse
	 * 0 Temes Huttner Degger Seber Lane vs. Mcnair Weber Schnabel Oleson Sanford
	 * ... 
	 * Your method will take in this file, and output the player (last name)
	 * with the most wins according to the file. A preceding 1 means the team
	 * before the "vs." won, and a 0 means the team before the "vs." lost.
	 * 
	 * Use a hashmap to accomplish this.
	 * 
	 * @param message
	 * @return result
	 */
	public static String compete(File tweets) throws FileNotFoundException {
		
		HashMap<String, Integer> stat = new HashMap<String, Integer>();
		
		Scanner scanner = new Scanner(tweets);
		while (scanner.hasNextLine()) {
			String line = scanner.nextLine();
			String elements[] = line.split("\\s+");
			if (elements[0].charAt(0) == '1') {
				for (int i = 0; i < 5; ++i) {
					if (stat.get(elements[i + 1]) == null)
						stat.put(elements[i + 1], 1);
					else
						stat.put(elements[i + 1], stat.get(elements[i + 1]) + 1);
				}
			} else if (elements[0].charAt(0) == '0') {
				for (int i = 0; i < 5; ++i) {
					if (stat.get(elements[i + 7]) == null)
						stat.put(elements[i + 7], 1);
					else
						stat.put(elements[i + 7], stat.get(elements[i + 7]) + 1);
				}
			}
		}
		scanner.close();
		
		String name = "";
		int max = 0;
		for (String key : stat.keySet()) {
			if (stat.get(key) > max) {
				max = stat.get(key);
				name = key;
			}
		}
		

		return name; // You should replace null with your String containing the
						// winner
	}
	
	
	// use this method to print out a map to debug your code
	public static void printMap(Map<String, Integer> m) {
		for (String s : m.keySet()) {
			System.out.println(s + ": " + m.get(s));
		}
	}

	public static void main(String[] args) throws FileNotFoundException {
		String reverseSent = printBack("Hello World! Look how far we've come!");
		// It should print: come weve far how Look World Hello
		System.out.println("Output of printBack() is : " + reverseSent);
		
		File tweets = new File("lab13test");
		String winner = compete(tweets);
		System.out.println("Winner is: " + winner);
	}
	 

}
